﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SHomeWorkshop.LunarSF.StudentInfosManager
{
    /// <summary>
    /// EcnomicInputer.xaml 的交互逻辑
    /// </summary>
    public partial class EcnomicInfosInputer : UserControl
    {
        public EcnomicInfosInputer()
        {
            InitializeComponent();

            this.tbxSavingCardNumber.DataContext =
                this.tbxSavingCardOwnerCid.DataContext =
                this.tbxSavingCardOwnerName.DataContext =
                this.cmbSavingCardOwnerRelationship.DataContext =
                this.cmbSavingCardBankName.DataContext =
                this.cmbStudentDeformityStatus.DataContext =
                this.cmbIsFiveGuaranteesFamily.DataContext =
                this.cmbIsBasicLivingAllowances.DataContext =
                this.cmbIsSingleParentChild.DataContext =
                this.cmbIsOrphan.DataContext =
                this.cmbIsDeformityParentsChild.DataContext =
                this.cmbStudentDeformityStatus.DataContext =
                this.cmbIsParentsCanNotWork.DataContext =
                this.cmbIsSeriousIllnessInTheFamily.DataContext =
                this.cmbIsArmyOrSpecialCaredChild.DataContext =
                this.tbxAnnualHouseholdIncome.DataContext =
                this.cmbSourceOfFamilyIncome.DataContext =
                this.cmbIsSufferFromNaturalDisasters.DataContext =
                this.tbxNaturalDisastersDiscription.DataContext =
                this.cmbIsUnexpectedAccidentHappened.DataContext =
                this.tbxUnexpectedAccidentDiscription.DataContext =
                this.tbxAmountOfDebt.DataContext =
                this.tbxDebtReson.DataContext =
                this.tbxFamilyMembersCount.DataContext =
                this.tbxSupportPersonsCount.DataContext =
                this.cmbIsRegistedPoorFamily.DataContext =
                this.cmbIsRegistedBasicLivingAllowances.DataContext =
                this.tbxOtherInfosFromCivilDepartment.DataContext =
                this.tbxLaborCountOfFamily.DataContext =
                this.tbxUnemployedCountOfFamily.DataContext =
                this.tbxOtherInfos.DataContext =

            //TODO: 每新加一个字段，都要在这里添加

            infosEntry;
        }

        public bool ErrorExist
        {
            get
            {
                foreach (UIElement ue in gridEcnomicInfos.Children)
                {
                    var tbx = ue as TextBox;
                    if (tbx == null) continue;

                    if (Validation.GetErrors(tbx).Count > 0) return true;
                }
                return false;
            }
        }

        private StudentEcnomicInfos infosEntry = new StudentEcnomicInfos();
        /// <summary>
        /// 表示一个学生的家庭经济状况。
        /// </summary>
        public StudentEcnomicInfos InfosEntry
        {
            get { return infosEntry; }
        }

        /// <summary>
        /// 此方法应该用在数据存入数据库后，重新初始化界面。
        /// </summary>
        public void ResetInfosEntry()
        {
            infosEntry = new StudentEcnomicInfos();
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F1)
            {
                LMessageBox.Show(InfosEntry.ToString(), Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else if (e.Key == Key.F3)
            {

            }
            else if (e.Key == Key.F4)
            {

            }
        }

        internal void ClearFields()
        {
            this.tbxSavingCardNumber.Text =
            this.tbxSavingCardOwnerCid.Text =
            this.tbxSavingCardOwnerName.Text = "";
            this.cmbSavingCardOwnerRelationship.SelectedIndex =
            this.cmbSavingCardBankName.SelectedIndex =
            this.cmbStudentDeformityStatus.SelectedIndex =
            this.cmbIsFiveGuaranteesFamily.SelectedIndex =
            this.cmbIsBasicLivingAllowances.SelectedIndex =
            this.cmbIsSingleParentChild.SelectedIndex =
            this.cmbIsOrphan.SelectedIndex =
            this.cmbIsDeformityParentsChild.SelectedIndex =
            this.cmbStudentDeformityStatus.SelectedIndex =
            this.cmbIsParentsCanNotWork.SelectedIndex =
            this.cmbIsSeriousIllnessInTheFamily.SelectedIndex =
            this.cmbIsArmyOrSpecialCaredChild.SelectedIndex = -1;
            this.tbxAnnualHouseholdIncome.Text = "";
            this.cmbSourceOfFamilyIncome.SelectedIndex =
            this.cmbIsSufferFromNaturalDisasters.SelectedIndex = -1;
            this.tbxNaturalDisastersDiscription.Text = "";
            this.cmbIsUnexpectedAccidentHappened.SelectedIndex = -1;
            this.tbxUnexpectedAccidentDiscription.Text = "";
            this.tbxAmountOfDebt.Text =
            this.tbxDebtReson.Text =
            this.tbxFamilyMembersCount.Text =
            this.tbxSupportPersonsCount.Text = "";
            this.cmbIsRegistedPoorFamily.SelectedIndex =
            this.cmbIsRegistedBasicLivingAllowances.SelectedIndex = -1;
            this.tbxOtherInfosFromCivilDepartment.Text =
            this.tbxLaborCountOfFamily.Text =
            this.tbxUnemployedCountOfFamily.Text =
            this.tbxOtherInfos.Text = "";
        }
    }
}
